﻿using AutoMapper;
using Entity.WMS.DTO.WarehousePurchase;
using Entity.WMS.DTO.WarehouseSale;
using Entity.WMS.Entity;
using Entity.WMS.View.WarehousePurchase;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using static Common.Enum.StateEnum;

namespace Service.WMS
{
    public class WarehousePurchaseService
    {
        /**
         * 数据库上下文
         */
        private readonly DataContext _context;

        /**
         * 自动映射器
         */
        private readonly IMapper _mapper;

        /**
         * 构造函数注入
         */
        public WarehousePurchaseService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取采购入库单列表
         */
        public async Task<RequestResponseModel<PageResult<WarehousePurchaseView>>> GetWarehousePurchaseList(WarehousePurchaseSearchDTO warehousePurchaseSearchDTO)
        {
            // 筛选
            var query = _context.WarehousePurchaseView
                .WhereIF(a => a.CardNo.Contains(warehousePurchaseSearchDTO.CardNo), !string.IsNullOrEmpty(warehousePurchaseSearchDTO.CardNo))
                .WhereIF(a => a.GoodsCode.Contains(warehousePurchaseSearchDTO.Goods) || a.GoodsName.Contains(warehousePurchaseSearchDTO.Goods) || a.GoodsSpec.Contains(warehousePurchaseSearchDTO.Goods), !string.IsNullOrEmpty(warehousePurchaseSearchDTO.Goods))
                .WhereIF(a => a.WarehouseCode.Contains(warehousePurchaseSearchDTO.Warehouse) || a.WarehouseName.Contains(warehousePurchaseSearchDTO.Warehouse), !string.IsNullOrEmpty(warehousePurchaseSearchDTO.Warehouse))
                .WhereIF(a => a.WarehousePositionCode.Contains(warehousePurchaseSearchDTO.WarehousePosition) || a.WarehousePositionName.Contains(warehousePurchaseSearchDTO.WarehousePosition), !string.IsNullOrEmpty(warehousePurchaseSearchDTO.WarehousePosition))
                ;

            // 返回列表
            var warehousePurchaseList = await query
                .OrderBy(a => a.CreateTime)
                .Page(warehousePurchaseSearchDTO.CurrentPage, warehousePurchaseSearchDTO.PageSize)
                .ToListAsync();

            // 计算总数
            var pageCount = await query.CountAsync();

            // 返回数据
            var pageResult = new PageResult<WarehousePurchaseView>(warehousePurchaseList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }


        /**
         * 入库
         */
        public async Task<RequestResponseModel<string>> Rk(string Id)
        {
            // 校验id
            var query = await Valid.ValidId(_context.WarehousePurchase, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            // 校验状态
            if (query.NumberStore >= query.NumberArrive)
            {
                return RequestResponse.CreateResponse(400, "已全部入库", Id);
            }
            query.NumberStore += query.NumberArrive;

            var item = await _context.PurchaseArriveOrderItem.FirstOrDefaultAsync(a => a.Id == query.PurchaseArriveOrderItemId);
            if (item != null)
            {
                var order = await _context.PurchaseArriveOrder.FirstOrDefaultAsync(a => a.Id == item.PurchaseArriveOrderId);
                if (order != null)
                {
                    order.Status = PurchaseType.已完成;
                }
                var warehouseBalance = await _context.WarehouseBalance.FirstOrDefaultAsync(a => a.GoodsId == item.GoodsId);
                if (warehouseBalance == null)
                {
                    var ware = new WarehouseBalanceModel()
                    {
                        Id = Guid.NewGuid().ToString(),
                        GoodsId = item.GoodsId,
                        Number_Begin = 0,
                        Number_End = item.NumberArrive,
                    };
                    _context.Add(ware);
                }
                else
                {
                    warehouseBalance.Number_End += item.NumberArrive;
                }
            }

            // 修改并保存数据库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "入库成功", Id);
        }
    }
}
